<?php
class Silk_Pms_Block_Adminhtml_gift_Edit extends Mage_Adminhtml_Block_Widget{
	private $_gift = null;

	public function __construct()
	{
		parent::__construct();
		$this->setTemplate('pms/gift/edit.phtml');
	}
	public function getGift(){
		if($this->_gift) return $this->_gift;

		$this->_gift = Mage::getModel('pms/exchange')->load($this->getRequest()->getParam('id'));

		if(!$this->_gift || !$this->_gift->getId())
		{
		    throw new Exception("活动不存在！");
		}
		return $this->_gift;
	}
	public function getOldQty($productId,$websiteId){
	    $website = Mage::getModel('core/website')->load($websiteId);
	    $storeId = $website->getDefaultStore()->getId();
	    $collection = Mage::getModel("catalog/product")->getCollection()->addAttributeToSelect('*');
	    $collection->addAttributeToFilter('entity_id',$productId);
	    if (Mage::helper('catalog')->isModuleEnabled('Mage_CatalogInventory')) {
	        $collection->joinField('qty',
	            'cataloginventory/stock_item',
	            'qty',
	            'product_id=entity_id',
	            '{{table}}.stock_id=1 AND {{table}}.website_id = ' . $websiteId,
	            'left');
	    }
	    $product = $collection->getFirstItem();
	    return $product->getQty();
	}
	public function hasStock($productId,$websiteId){
		$collection = Mage::getModel("catalog/product")->getCollection()->addAttributeToSelect('*');
		$collection->addAttributeToFilter('entity_id',$productId);
		if (Mage::helper('catalog')->isModuleEnabled('Mage_CatalogInventory')) {
		    $collection->joinField('is_in_stock',
		        'cataloginventory/stock_item',
		        'is_in_stock',
		        'product_id=entity_id',
		        '{{table}}.stock_id=1 AND {{table}}.website_id = ' . $websiteId,
		        'left');
		}
		$product = $collection->getFirstItem();
		return $product->getIsInStock();
	}
	public function getImageId($id)
	{
	    try {

	        $product = Mage::getModel('catalog/product')->load($id);

	        $websiteId = Mage::getSingleton('admin/session')->getUser()->getWebsiteId();

	        if($websiteId){
	            $storeId = Mage::getModel('core/website')->load($websiteId)->getDefaultStore()->getId();
	            $product = Mage::getModel('catalog/product')->setStoreId($storeId)->load($id);
	            $productImg = Mage::helper('catalog/image')->init($product,'small_image')->resize(100);
	        }else{
	            $productImg = Mage::helper('catalog/image')->init($product,'small_image')->resize(100);
	        }
	    } catch (Exception $e){

	        Mage::logException($e);

	        $placeHolderImg = Mage::getStoreConfig("catalog/placeholder/small_image_placeholder");

	        if(empty($placeHolderImg)){

	            $productImg = "";

	        }else{

	            $productImg = Mage::getSingleton('catalog/product_media_config')->
	                getBaseMediaUrl(). '/placeholder/' . $placeHolderImg;

	        }

	    }


	    return '<img src="'.$productImg.'">';
	}
	public function getProductsku($id){
		$product = Mage::getModel("catalog/product")->load($id);
		return $product->getSku();
	}
}